package cn.com.nes.site.controller.comecall;

import cn.com.nes.common.util.SysResult;
import cn.com.nes.common.web.CommonResult;
import cn.com.nes.mybatis.callcenter.mapper.CompanyAttachMapper;
import cn.com.nes.site.entity.vo.comecall.CallExtensionVO;
import cn.com.nes.site.entity.vo.comecall.ClienteleInfoByPhoneVo;
import cn.com.nes.site.entity.vo.comecall.ComecallVo;
import cn.com.nes.site.service.comecall.CallExtensionService;
import cn.com.nes.site.service.comecall.dto.CallExtensionDto;
import cn.com.nes.site.service.comecall.entity.CompanyAttach;
import cn.com.nes.site.service.crm.customer.CrmCustomerService;
import cn.com.nes.site.service.system.callCenter.CallCenterService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = {"项目模块 -呼叫中心"})
@Controller
@RequestMapping("/v2/comecall")
public class ComecallController {

    @Resource
    private CompanyAttachMapper companyAttachMapper;
    @Resource
    private CallExtensionService callExtensionService;


    @Resource
    private CrmCustomerService crmCustomerService;
    @Resource
    private CallCenterService callCenterService;

    @RequestMapping(value = "/forward", method = RequestMethod.GET)
    @ResponseBody
    public SysResult forward(HttpServletRequest request,ComecallVo vo) throws Exception {
        return SysResult.ok(callCenterService.forward(request.getQueryString(), vo));
    }

    /**
     * 分机列表
     *
     * @return
     */
    @RequestMapping(value = "/listExtension", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CallExtensionDto> listExtension(@RequestBody CallExtensionVO vo) {
        Page<CallExtensionDto> page = null;
        try {
            CompanyAttach companyAttach = companyAttachMapper.queryCompanyAttach(vo.getOperateUserCallCenterCompanyId());
            vo.setCompanyAttachId(companyAttach.getId());
            page = callExtensionService.getExtensions(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(e.getMessage());
        }
        return CommonResult.page(page);
    }

    @RequestMapping(value = "/bindExtension", method = RequestMethod.POST)
    @ResponseBody
    public SysResult bindExtension(@RequestBody CallExtensionVO vo) {
        try {
            if (StringUtils.isEmpty(vo.getAccount())) {
                vo.setAccount("");
            }
            CompanyAttach companyAttach = companyAttachMapper.queryCompanyAttach(vo.getOperateUserCallCenterCompanyId());
            vo.setCompanyAttachId(companyAttach.getId());
            vo.setAccount(vo.getOperateUserName());
            if (callExtensionService.bindExtension(vo) == 0) {
                return SysResult.ok("绑定失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.error("绑定失败");
        }

        return SysResult.ok("绑定成功");
    }


    @RequestMapping(value = "/unBindExtension", method = RequestMethod.POST)
    @ResponseBody
    public SysResult unBindExtension(@RequestBody CallExtensionVO vo) {

        try {
            CompanyAttach companyAttach = companyAttachMapper.queryCompanyAttach(vo.getOperateUserCallCenterCompanyId());
            vo.setCompanyAttachId(companyAttach.getId());
            if (callExtensionService.unBindExtension(vo) == 0) {
                return SysResult.ok("解绑失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return SysResult.error("解绑失败");
        }

        return SysResult.ok("解绑成功");
    }

    /**
     * 根据手机号查询用户信息
     *
     */
    @RequestMapping(value = "/getClienteleInfoByPhone", method = RequestMethod.POST)
    @ResponseBody
    public SysResult getClienteleInfoByPhone(@RequestBody ClienteleInfoByPhoneVo vo) throws Exception {
        if(ObjectUtil.isNotEmpty(crmCustomerService.getCustomerByPhone(vo.getPhone(),vo.getCompanyId()))) {
            return SysResult.ok(crmCustomerService.getCustomerByPhone(vo.getPhone(), vo.getCompanyId()));
        }else{
            return SysResult.error("查询失败");

        }    }

}
